﻿using System;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.Exceptions;
using Jayden.Dll.Web.HtmlControls.Descriptions;
using Jayden.Dll.Web.HtmlControls.Generics;
using Jayden.Dll.Web.HtmlControls.Interfaces;

namespace Jayden.Dll.Web.HtmlControls
{
	public class Img : HtmlElement, WithSrc, WithAlt, WithWidth, WithHeight
	{
		public Img() : base(ImgDescription.Instance) { }
		public Img(string alt, string src) : this()
		{
			Alt = alt;
			Src = src;
		}

		public string Src { get { return this["src"]; } set { this["src"] = value; } }
		public string Alt { get { return this["alt"]; } set { this["alt"] = value; } }
		public uint? Width { get { return GetAttributeUint("width"); } set { SetAttribute("width", value); } }
		public uint? Height { get { return GetAttributeUint("height"); } set { SetAttribute("height", value); } }
		public string UseMap { get { return this["usemap"]; } set { this["usemap"] = value; } }
		public bool IsMap { get { return GetAttributeBool("ismap"); } set { SetAttribute("ismap", value); } }

		protected override void TranslateAttributes(Core.Translations.ITranslator translator)
		{
			base.TranslateAttributes(translator);
			Alt = translator.Translate(Alt);
		}
	}
	public class ImgDescription : HtmlElementDescription
	{
		public static readonly ImgDescription Instance = new ImgDescription();
		private ImgDescription() : base("img", HtmlVersion.Html4, false, false) { }
		public override Type ControlType { get { return typeof(Img); } }
		public override bool IsPhrasingElement { get { return true; } }
		public override string Description { get { return "image."; } }
		public override bool CheckValidity(HtmlControl control)
		{
			if (base.CheckValidity(control))
			{
				Img element = control as Img;
				if (element.Alt == null)
					throw new HtmlException("{0} must have an 'alt' attribute.", this);
				return true;
			}
			return false;
		}
	}
}
